package com.probiz.estoresa.customer.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.event.StoreFrontCache4ProductClearEvent;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.model.ConsultSearchCriteria;
import com.probiz.estore.customer.service.ConsultManager;
import com.probiz.estore.system.service.MsgSendManager;
import com.probiz.estore.system.util.MsgTemplateConstants;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

public class ConsultController extends GenericStoreAdminController<Consult> {
    private ConsultManager consultManager = null;
    private MsgSendManager msgSendManager = null;

    public void setConsultManager(ConsultManager inMgr) {
        this.consultManager = inMgr;
    }

	public void setMsgSendManager(MsgSendManager msgSendManager) {
		this.msgSendManager = msgSendManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Consult entity) {
		return entity.getConsultName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = consultManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#onSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, Consult entity, BindException errors) {
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/consultList");
		return mv;
	}
	
	public ModelAndView search(HttpServletRequest request,	HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/include/consultListBody");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		ConsultSearchCriteria consultSearchCriteria = new ConsultSearchCriteria();
		try {
			// 绑定参数
			bind(request, consultSearchCriteria);
			List<Consult> consultList = consultManager.searchByCriteriaForController(consultSearchCriteria,searchCriteria);
			mv.addObject("consultList", consultList);
			mv.addObject("consultSearchCriteria", consultSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	/**
	 * 切换已阅未阅
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleReadStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Consult consult = consultManager.getById(Integer.valueOf(entityId));
				if(!consult.getIsRead().equals(Consult.STATUS_READ)){
					consultManager.doMarkRead(consult);
				}else{
					consultManager.doMarkUnRead(consult);
				}
			}
		}
		return null;
	}
	/**
	 * 切换可见隐藏
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleShowStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Consult consult = consultManager.getById(Integer.valueOf(entityId));
				if(!consult.getStatus().equals(Consult.STATUS_SHOW)){
					consultManager.doMarkShow(consult);
				}else{
					consultManager.doMarkUnShow(consult);
				}
				//清空该商品页面缓存
				EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,consult.getProductId()));
			}
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Consult consult = consultManager.getById(Integer.valueOf(entityId));
				consultManager.deleteFully(consult);

				//清空该商品页面缓存
				EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,consult.getProductId()));
			}
			
		}
		return null;
	}
	/**
	 * 置为已读
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView markRead(HttpServletRequest request,	HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		Consult consult= consultManager.getById(RequestUtil.getInteger(request, "consultId"));
		if(!consult.getIsRead().equals(Consult.STATUS_READ)){
			consultManager.doMarkRead(consult);
		}
		data.put("consultId", consult.getConsultId());
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 管理员回复
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView replyConsult(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Consult consult = consultManager.getById(RequestUtil.getInteger(request, "consultId"));
		Consult reply = new Consult();
		reply.setParent(consult);
		reply.setCustomer((AppUser) RequestContext.getCurrentUser());
		reply.setRemoteIp(request.getRemoteAddr());
		reply.setProduct(consult.getProduct());
		reply.setMessage(RequestUtil.getParameterNullSafe(request, "replyMessage"));
		reply.setIsRead(Consult.STATUS_READ);
		reply.setSortOrder(Consult.STATUS_UNTOP);
		reply.setStatus(Consult.STATUS_SHOW);
		consultManager.addReply(reply);
		
		if(consult.getCustomerId() != null){
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("consult", consult);
			model.put("reply", reply);
			msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_CONSULT_REPLY, model, new String[]{consult.getCustomer().getEmail()}, consult.getCustomerId() == null ? null : new String[]{consult.getCustomerId().toString()}, new String[]{consult.getCustomer().getMobilephone()});
		}
			
		data.put("consultId", consult.getConsultId());//返回评论的id
		
		//清空该商品页面缓存
		EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,consult.getProductId()));
		
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 获得评论
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView getConsult(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Consult consult = consultManager.getById(RequestUtil.getInteger(request, "consultId"));
		data.put("consultId", consult.getConsultId());
		data.put("replyCount", consult.getReplyCount());
		data.put("onShowReplyCount", consult.getOnShowReplyCount());
		return this.getModelAndAjaxJsonView(data);
	}

}
